*** Rotate table: code adapted from http://repec.org/bocode/e/estout

	capture program drop widetable
	program widetable
	
		*** core syntax is just the list of dep vars from regressions
		*** Syntax presupposes that regression estimates are stored under names row1 through rowK
		version 11
		syntax varlist , File(string) [Mlabels(passthru) Mtitles(passthru)]
		tokenize `varlist'

		*** esttab requires variables to exist for each row
		global rows = ""
		local i = 1
		foreach v of varlist `varlist'{
			gen row`i' = .
			label variable row`i' "`: variable label `v''"
			global rows = "$rows row`i'"
			local ++i
		}
		
		esttab $rows, se
		matrix C = r(coefs)
		matrix S = r(stats)
		eststo clear
		local rnames : rownames C
		local models : coleq C
		local models : list uniq models
		local i 0
		foreach name of local rnames {
			local ++i
			local j 0
			capture matrix drop b
			capture matrix drop se
			foreach model of local models {
				local ++j
				matrix tmp = C[`i', 3*`j'-2]
				if tmp[1,1]<. {
					matrix colnames tmp = `model'
					matrix b = nullmat(b), tmp
					matrix tmp[1,1] = C[`i', 3*`j'-1]
					matrix se = nullmat(se), tmp
				}
			}
			ereturn post b
			quietly estadd matrix se
			eststo `name'
		}
		local snames : rownames S
		local i 0
		foreach name of local snames {
			local ++i
			local j 0
			capture matrix drop b
			foreach model of local models {
				local ++j
				matrix tmp = S[`i', `j']
				matrix colnames tmp = `model'
				matrix b = nullmat(b), tmp
			}
			ereturn post b
			eststo `name'
		}
	
	*** After this must delete first \addlinespace & last row
		esttab using "$figures/`file'.tex", 						///
			f booktabs eqlabels(none) label se(3) b(3) 				///
			nodep nonotes nolines noobs 							///
			starlevels(* 0.10 ** 0.05 *** 0.01) se gaps replace		///	
			mlabels("`mlabels'") mtitles("`mtitles'")
			
	end
